using System.Collections;
using System.Globalization;
using System.IO;

namespace BoxBinary.AspectCache.Core.Helpers
{
    /// <summary>
    /// 
    /// </summary>
    public class FileInfoComparer : IComparer
    {
        #region SortType enum

        /// <summary>
        /// 
        /// </summary>
        public enum SortType
        {
            /// <summary>
            /// 
            /// </summary>
            CreationTime
        }

        #endregion

        private readonly bool _descending;
        private readonly SortType _sortType = SortType.CreationTime;

        /// <summary>
        /// Initializes a new instance of the <see cref="FileInfoComparer"/> class.
        /// </summary>
        /// <param name="sortType">Type of the sort.</param>
        /// <param name="descending">if set to <c>true</c> [descending].</param>
        public FileInfoComparer(SortType sortType, bool descending)
        {
            _sortType = sortType;
            _descending = descending;
        }

        #region IComparer Members

        /// <summary>
        /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// <param name="x">The first object to compare.</param>
        /// <param name="y">The second object to compare.</param>
        /// <returns>
        /// Value Condition Less than zero <paramref name="x"/> is less than <paramref name="y"/>. Zero <paramref name="x"/> equals <paramref name="y"/>. Greater than zero <paramref name="x"/> is greater than <paramref name="y"/>.
        /// </returns>
        /// <exception cref="T:System.ArgumentException">Neither <paramref name="x"/> nor <paramref name="y"/> implements the <see cref="T:System.IComparable"/> interface.-or- <paramref name="x"/> and <paramref name="y"/> are of different types and neither one can handle comparisons with the other. </exception>
        public int Compare(object x, object y)
        {
            var castX = (FileInfo) x;
            var castY = (FileInfo) y;
            switch (_sortType)
            {
                case SortType.CreationTime:
                    int compareValue = new Comparer(CultureInfo.InvariantCulture).Compare(castX.CreationTime,
                                                                                          castY.CreationTime);
                    return _descending ? compareValue*-1 : compareValue;
            }
            return 0;
        }

        #endregion
    }
}